/*
 * @(#)XmlDbDataSource.java 0.0.1-SNAPSHOT
 *
 * Copyright Group B
 */

package grupb.upf.DBdatasource.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xmldb.api.base.XMLDBException;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Database;
import org.xmldb.api.DatabaseManager;

/**
 * <p>This interface implements the connection with the database.</p>
 *
 * @version    0.0.1-SNAPSHOT
 * @author     Group B
 */
public final class XmlDbDataSource implements grupb.upf.DBdatasource
        .XmlDbDataSource {

    /**
     * Stores the logs.
     */
    private static final Log LOG = LogFactory.getLog(XmlDbDataSource.class);
    /**
     * This method gets the db properties file.
     * @throws Exception we are not able to load it
     */
    public XmlDbDataSource() throws Exception {
        Class cl = Class.forName(grupb.upf.util.Config.getProperty(
                "XmlDbDataSource/driverClassName"));
        Database database = (Database) cl.newInstance();
        database.setProperty("create-database", "true");
        DatabaseManager.registerDatabase(database);
    }
    /**
     * This method returns the username and password from the properties file.
     * @return The user and password.
     * @throws org.xmldb.api.base.XMLDBException Throws and exception
     */
    @Override
    public Collection getCollection() throws XMLDBException {
        return getCollection(grupb.upf.util.Config.getProperty(
                "XmlDbDataSource/user"), grupb.upf.util.Config.getProperty(
                "XmlDbDataSource/password"));
    }
    /**
     * This method return the properties.
     * @param aUserName The username
     * @param aPassword the password
     * @return The properties
     * @throws org.xmldb.api.base.XMLDBException The exception
     */

    @Override
    public Collection getCollection(final String aUserName,
            final String aPassword) throws XMLDBException {
        return DatabaseManager.getCollection(grupb.upf.util.Config.getProperty(
                "XmlDbDataSource/url"), aUserName, aPassword);
    }
}
